// subPages/mine/myDevice/myDevice.js
const app = getApp()
var json =  require('../../../data/json.js');
var util =  require('../../../utils/util.js');
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceList: json.deviceList
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getData()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getData()
        }
      }
    }
  },

  //获取数据
  getData:function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/get_user_bd',
      data: {
        userid: that.data.userId
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.length != 0){
          var list = that.data.deviceList
          for(var i in res.data){
            switch (res.data[i].type) {
              case '1':
                that.data.deviceList[0].deviceId = 'ID:' + res.data[i].eid
                break;
              case '2':
                that.data.deviceList[1].deviceId = 'ID:' + res.data[i].eid
                break;
              case '3':
                that.data.deviceList[2].deviceId = 'ID:' + res.data[i].eid
                break;
            }
          }
          that.setData({
            deviceList: list
          })
        }
      },
    })
  },

  //选择模块
  chageModule:function(e){
    var that = this
    var index = e.currentTarget.dataset.index;
    var list = that.data.deviceList
    for(var i in list){
      if(index == list[i].index){
        list[i].isShow = true 
      }
      else
      {
        list[i].isShow = false 
      }
    }
    that.setData({
      deviceList: list
    })
  },

  //绑定设备input
  numberInput:function(e){
    this.setData({
      number : e.detail.value
    })
  },

  //扫描二维码
  clickBtn:function(){
    var that = this
    wx.scanCode({
      onlyFromCamera: true,
      scanType:'qrCode',
      success (res) {
        // console.log(res)
        that.setData({
          number : res.result
        })
      }
    })
  },

  //获取type
  getType:function(){
    var that = this
    var list = that.data.deviceList
    for(var i in list){
      if(list[i].isShow){
        return list[i].index
      }
    }
    return false
  },

  //绑定
  clickBtn2:function(){
    var that = this
    var type = that.getType()
    if(flag){
      return
    }
    flag = true
    if(!type){
      util.showModal('提示','请选择要绑定的设备')
      flag = false
      return
    }
    if(!that.data.number){
      util.showModal('提示','请输入设备编号')
      flag = false
      return
    }
    // console.log(that.data.number)
    // console.log(type)
    util.postAjax(app.globalData.apiUrl +'/user_bd',function(res){
      // console.log(res)
      if(res.data.code == '200'){
        util.showToast('绑定成功','success',2000)
        that.getData()
      }else{
        util.showModal('提示','设备绑定失败，请重新绑定')
      }
      flag = false
    },{userid: that.data.userId,eId : that.data.number,type: type})
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})